package com.changpay.zywy.ui.dialog;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.changpay.zywy.R;
import com.ruffian.library.widget.RCheckBox;
import com.ruffian.library.widget.RTextView;
import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by AndroidIntexh1 on 2018/12/26.
 */

public class InputCodeDialog extends BaseCustomDialog {

    public OnDialogClickListener dialogListener;
    private RCheckBox cb_one, cb_two, cb_three, cb_four, cb_five, cb_six;
    private RTextView number_one, number_two, number_three, number_four, number_five;
    private RTextView number_six, number_seven, number_eight, number_nine, number_zero;
    private RTextView number_sure, number_back;
    private ImageView iv_close;

    private RTextView check_code;
    private TextView check_message, check_success;

    private StringBuilder stringBuilder = new StringBuilder();
    private List<RCheckBox> checkBoxList = new ArrayList<>();
    private static final String KEY_TYPE = "KEY_TYPE";

    private View.OnClickListener onClickListener = v -> {
        switch (v.getId()) {
            case R.id.number_one:
                takePlay("1");
                break;
            case R.id.number_two:
                takePlay("2");
                break;
            case R.id.number_three:
                takePlay("3");
                break;
            case R.id.number_four:
                takePlay("4");
                break;
            case R.id.number_five:
                takePlay("5");
                break;
            case R.id.number_six:
                takePlay("6");
                break;
            case R.id.number_seven:
                takePlay("7");
                break;
            case R.id.number_eight:
                takePlay("8");
                break;
            case R.id.number_nine:
                takePlay("9");
                break;
            case R.id.number_zero:
                takePlay("0");
                break;
            case R.id.number_sure:
                takePlay("sure");
                break;
            case R.id.number_back:
                takePlay("back");
                break;
            case R.id.iv_close:
                this.dismiss();
                break;
            case R.id.check_code:
                getCode();
                break;
        }
    };

    private void getCode() {
        dialogListener.getSMSCode(check_code,check_message,check_success);
    }



    /**
     * 自定义builder来增加一些参数，记得要继承自BaseEasyDialog.Builder
     */
    public static class Builder extends BaseEasyDialog.Builder<InputCodeDialog.Builder> {
        private Bundle bundle = new Bundle();
        private InputCodeDialog.OnDialogClickListener listener;

        public Builder(@NonNull Context context) {
            super(context);
        }


        public InputCodeDialog.Builder setOnDialogClickListener(InputCodeDialog.OnDialogClickListener lis) {
            listener = lis;
            return this;
        }

        @NonNull
        @Override
        protected InputCodeDialog createDialog() {
            InputCodeDialog dialog = new InputCodeDialog();
            dialog.setArguments(bundle);
            dialog.dialogListener = listener;
            return dialog;
        }

        public InputCodeDialog.Builder setType(int type) {
            bundle.putInt(KEY_TYPE, type);
            return this;
        }


    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_input_code;
    }

    @Override
    protected void bindViews(View view) {

        cb_one = findView(R.id.cb_one);
        cb_two = findView(R.id.cb_two);
        cb_three = findView(R.id.cb_three);
        cb_four = findView(R.id.cb_four);
        cb_five = findView(R.id.cb_five);
        cb_six = findView(R.id.cb_six);

        number_one = findView(R.id.number_one);
        number_two = findView(R.id.number_two);
        number_three = findView(R.id.number_three);
        number_four = findView(R.id.number_four);
        number_five = findView(R.id.number_five);

        number_six = findView(R.id.number_six);
        number_seven = findView(R.id.number_seven);
        number_eight = findView(R.id.number_eight);
        number_nine = findView(R.id.number_nine);
        number_zero = findView(R.id.number_zero);

        number_sure = findView(R.id.number_sure);
        number_back = findView(R.id.number_back);

        iv_close = findView(R.id.iv_close);

        check_code = findView(R.id.check_code);
        check_message = findView(R.id.check_message);
        check_success = findView(R.id.check_success);

        checkBoxList.add(cb_one);
        checkBoxList.add(cb_two);
        checkBoxList.add(cb_three);
        checkBoxList.add(cb_four);
        checkBoxList.add(cb_five);
        checkBoxList.add(cb_six);

        number_one.setOnClickListener(onClickListener);
        number_two.setOnClickListener(onClickListener);
        number_three.setOnClickListener(onClickListener);
        number_four.setOnClickListener(onClickListener);
        number_five.setOnClickListener(onClickListener);
        number_six.setOnClickListener(onClickListener);
        number_seven.setOnClickListener(onClickListener);
        number_eight.setOnClickListener(onClickListener);
        number_nine.setOnClickListener(onClickListener);
        number_zero.setOnClickListener(onClickListener);
        number_sure.setOnClickListener(onClickListener);
        number_back.setOnClickListener(onClickListener);
        check_code.setOnClickListener(onClickListener);
        iv_close.setOnClickListener(onClickListener);
    }

    private void setListener() {

    }

    private void setData() {

    }

    @Override
    protected void setViews() {
        setData();
        setListener();
        setBackground();
        setLayout();

    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
//        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_shop_jion_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        // 强制宽高
        lp.width = getScreenWidth(getActivity());
        lp.height = getScreenHeight(getActivity());

        lp.gravity = Gravity.TOP; // 设置展示的位置
        window.setAttributes(lp);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    public static int getScreenHeight(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 监听输入框内容
     */
    public interface OnDialogClickListener {
        /**
         * Called when a view has been clicked.
         */
        void onClick(String type);

        void getSMSCode(View view,View two,View three);
    }


    private void takePlay(String type) {
        if (type.equals("back")) {
            if (stringBuilder.toString().isEmpty()) return;
            stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
            checkBoxList.get(stringBuilder.length()).setChecked(false);

        } else if (type.equals("sure")) {
            if (stringBuilder.length() < 6) {
                Toast.makeText(getContext(), "长度不足6位", Toast.LENGTH_SHORT).show();
                return;
            }
            dialogListener.onClick(stringBuilder.toString());
            this.dismiss();
        } else {
            if (stringBuilder.length() >= 6) return;
            stringBuilder.append(type);
            checkBoxList.get(stringBuilder.length() - 1).setChecked(true);
        }
    }
}
